package epoch.game.core.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;

/**
 * 文件工具类
 * 
 * @author Lpc丶Sng
 *
 * @date 2016年4月10日 下午11:08:12
 */
public class FileUtil {

	/**
	 * 解析内容
	 * 
	 * @param path
	 * @return
	 */
	public static String parseContent(String path) {
		if (path == null)
			return null;
		path = "../" + path;
		InputStream is = ClassLoader.getSystemResourceAsStream(path);
		if (is == null)
			return null;
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		StringBuffer buff = new StringBuffer();
		try {
			while (br.ready()) {
				buff.append(br.readLine()).append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buff.toString();
	}

	/**
	 * 读取文本文件
	 * 
	 * @param path
	 * @return
	 */
	public static String read(String path) {
		String res = null;
		InputStream is = null;
		try {
			is = new FileInputStream(new File(path));
		} catch (FileNotFoundException e) {
			return null;
		}
		if (is != null) {
			res = read(is);
			try {
				is.close();
			} catch (IOException e) {

			}
		}
		return res;
	}

	/**
	 * 读取内容
	 * 
	 * @param is
	 * @return
	 */
	public static String read(InputStream is) {
		if (is == null)
			return "";
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			while (br.ready()) {
				sb.append(br.readLine()).append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

	/**
	 * 保存文本文件
	 * 
	 * @param path
	 * @param content
	 */
	public static void save(String path, String content) {
		File file = new File(path);
		try {
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
			osw.write(content);
			osw.flush();
			osw.close();
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 自动创建文件夹
	 * 
	 * @param path
	 */
	public static void mkdirs(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	/**
	 * 生成目录及文件
	 * 
	 * @param path
	 * @return
	 */
	public static void makeDirFile(String path) {
		File file = new File(path);
		if (file.exists())
			return;
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除子文件
	 * 
	 * @param dir
	 */
	public static void deleteFiles(File dir) {
		File[] files = dir.listFiles();
		if (files == null)
			return;
		for (File item : files) {
			if (item.isFile()) {
				item.delete();
			}
		}
	}

	/**
	 * 读取配置文件
	 * 
	 * @param path
	 * @return
	 */
	public static Properties loadProperties(String path) {
		InputStream in = null;
		Properties properties = new Properties();
		try {
			in = new FileInputStream(new File(path));
			properties.load(in);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {

				}
			}
		}
		return properties;
	}

	/**
	 * 运行BAT文件
	 * 
	 * @param path
	 */
	public static void runBat(String path) {
		try {
			Process ps = Runtime.getRuntime().exec(path);
			InputStream in = ps.getInputStream();
			// int c;
			// while ((c = in.read()) != -1) {
			// System.out.print(c);
			// }
			in.close();
			ps.waitFor();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
